package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.StringUtils;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysPermission;
import cn.wgx.modules.sys.entity.SysPermissionExample;
import cn.wgx.modules.sys.entity.mapper .SysMenuMapper;
import cn.wgx.modules.sys.entity .SysMenu;
import cn.wgx.modules.sys.entity .SysMenuExample;

import cn.wgx.modules.sys.entity.mapper.SysPermissionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class SysMenuService extends BaseService<SysMenu> {

    @Resource
    SysMenuMapper mapper;

    @Resource
    SysPermissionMapper sysPermissionMapper;

    @Transactional(readOnly = true)
    public List<SysMenu> getList(SysMenu record, Boolean desc) {
		SysMenuExample example = new SysMenuExample();
		if(record != null){
            SysMenuExample.Criteria criteria = example.createCriteria();
            if(StringUtils.isNotBlank(record.getTitle())){
                criteria.andTitleLike("%" + record.getTitle() + "%");
            }
        }
		example.setOrderByClause("createDate " + (desc?"desc": "asc"));
		return mapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SysMenu> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysMenuExample example = new SysMenuExample();
            SysMenuExample.Criteria criteria = example.createCriteria();
            criteria.andMenu_idIn(uniList);
            List<SysMenu> eduCourseAssignmentHis = mapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    @Transactional(readOnly = true)
    public SysMenu getOne(Integer id) {
		return mapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    public List<SysPermission> getPerByMenuId(Integer menu_id) {
        if(Validation.isNotNullOrZero(menu_id)){
            SysPermissionExample example = new SysPermissionExample();
            SysPermissionExample.Criteria criteria = example.createCriteria();
            addCustomSQL(criteria, "t.permission_id in (select permission_id from sys_menu_permission where menu_id = "+menu_id+") and 1=", "1");
            return sysPermissionMapper.selectByExample(example);
        }else{
            return emptyList;
        }
    }


    @Transactional
    public SysMenu insertOne( SysMenu record) {
        filter(record, true);
        mapper.insertSelective(record);
        return record;
    }


    @Transactional
    public SysMenu updateOne( SysMenu record) {
        filter(record, false);
        mapper.updateByPrimaryKeySelective(record);
        return record;

    }


    @Transactional
    public int deleteOne( Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

	public void filter(SysMenu record, boolean insert){
        if(insert){

        }else{
            record.setTopPid(null);
            record.setPid(null);
        }
        super.filter(record, insert);
    }

}